package cz.proks.easyhelpdesk.gae.api.impl.dbo;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.ConvertibleDbo;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.IdentifiableModelDbo;


public class HelpdeskMember extends cz.proks.easyhelpdesk.api.model.HelpdeskMember implements IdentifiableModelDbo, ConvertibleDbo<cz.proks.easyhelpdesk.api.model.HelpdeskMember>{            
    private User user;         
    private Helpdesk helpdesk;   
    private List<cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole> roles;
    
    @Override
    public Long getUserId() {        
        return super.getUserId();
    }

    @Override
    public void setUserId(Long userId) {
        super.setUserId(userId);
        
        if (userId == null)
            user = null;
        else {
            user = new User();      
            user.setId(userId);
        }        
    }
    
    @Override
    public Long getHelpdeskId() {    
        return super.getHelpdeskId();
    }

    @Override
    public void setHelpdeskId(Long helpdeskId) {
        super.setHelpdeskId(helpdeskId);
        
        if (helpdeskId == null)
            helpdesk = null;            
        else {
            helpdesk = new Helpdesk();        
            helpdesk.setId(helpdeskId);
        }                           
    }        
    
    @Override
    public List<HelpdeskRole> getHelpdeskRoles() {        
        return super.getHelpdeskRoles();
    }

    @Override
    public void setHelpdeskRoles(List<HelpdeskRole> helpdeskRoles) {        
        super.setHelpdeskRoles(helpdeskRoles);
        
        roles = new LinkedList<cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole>();
        if (helpdeskRoles != null)
            for (HelpdeskRole helpdeskRole : helpdeskRoles) {
                
                cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole dboRole = new cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole();
                dboRole.setId(Long.valueOf(helpdeskRole.ordinal()));
                dboRole.setRole(helpdeskRole);
                
                roles.add(dboRole);
            }
    }

    public Helpdesk getHelpdesk() {
        return helpdesk;
    }    
      
    public void setHelpdesk(Helpdesk helpdesk) {
        this.helpdesk = helpdesk;
        super.setHelpdeskId(helpdesk != null ? helpdesk.getId() : null);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        super.setUserId(user != null ? user.getId() : null);
    }
    
    public List<cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole> getRoles() {
        return roles;
    }

    public void setRoles(List<cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole> roles) {
        this.roles = roles;
        
        List<HelpdeskRole> helpdeskRoles = new LinkedList<HelpdeskRole>();
        if (roles != null)
            for(cz.proks.easyhelpdesk.gae.api.impl.dbo.HelpdeskRole dboRole : roles)
                helpdeskRoles.add(dboRole.getRole());
        
        super.setHelpdeskRoles(helpdeskRoles);
    }

    @Override
    public void toDbo(cz.proks.easyhelpdesk.api.model.HelpdeskMember domain) {
        BeanUtils.copyProperties(domain, this);
    }

    @Override
    public cz.proks.easyhelpdesk.api.model.HelpdeskMember asDomain() {
        cz.proks.easyhelpdesk.api.model.HelpdeskMember helpdeskMember = new cz.proks.easyhelpdesk.api.model.HelpdeskMember();
        BeanUtils.copyProperties(this, helpdeskMember, new String[] {"user", "helpdesk"});        
        return helpdeskMember;
    }               
}
